package com.hg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hg.mapper.ContentCategoryMapper;
import com.hg.mapper.ContentMapper;
import com.hg.pojo.Content;
import com.hg.pojo.ContentCategory;
import com.hg.service.IContentService;
import com.hg.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Baomidou
 * @since 2025-07-31
 */
@Service
@Transactional
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements IContentService {
    @Autowired
    private ContentCategoryMapper contentCategoryMapper;
    @Autowired
    private ContentMapper contentMapper;

    @Override
    public List<ContentCategory> selectContentCategoryByParentId(Long id) {
        QueryWrapper<ContentCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        queryWrapper.ne("status", 2);
        List<ContentCategory> contentCategoryList = contentCategoryMapper.selectList(queryWrapper);
        return contentCategoryList;
    }

    @Override
    public void insertContentCategory(Long parentId, String name) {
        ContentCategory contentCategory = new ContentCategory();
        contentCategory.setParentId(parentId);
        if(parentId == 0){
            ContentCategory contentCategoryParent = contentCategoryMapper.selectById(parentId);
            contentCategoryParent.setIsParent(true);
        }
        contentCategory.setName(name);
        contentCategory.setStatus(1);
        contentCategory.setCreated(new Date());
        contentCategory.setUpdated(new Date());
        contentCategoryMapper.insert(contentCategory);
    }

    @Override
    public void deleteContentCategoryById(Long categoryId) {
        ContentCategory contentCategory = contentCategoryMapper.selectById(categoryId);
        Boolean isParent = contentCategory.getIsParent();
        if (isParent){
            throw new RuntimeException("该节点有子节点，不能删除");
        }
        contentCategory.setStatus(2);
        contentCategory.setIsParent(false);
        contentCategory.setUpdated(new Date());
        contentCategoryMapper.updateById(contentCategory);
    }

    @Override
    public void updateContentCategory(Long id,String name) {
        ContentCategory contentCategory = new ContentCategory();
        contentCategory.setId(id);
        contentCategory.setName(name);
        contentCategory.setUpdated(new Date());
        contentCategoryMapper.updateById(contentCategory);
    }

    @Override
    public PageResult selectTbContentAllByCategoryId(Long categoryId, Integer page, Integer rows) {
        Page<Content> pageParam = new Page<>(page,rows);
        Page<Content> contentPage = contentMapper.selectPage(pageParam, null);
        return new PageResult(contentPage.getCurrent(), contentPage.getPages(), contentPage.getRecords());
    }

    @Override
    public void insertTbContent(Content content) {
        content.setCreated(new Date());
        content.setUpdated(new Date());
        contentMapper.insert(content);

    }

    @Override
    public void deleteContentByIds(Long ids) {
        contentMapper.deleteById(ids);
    }
}
